package demo;

import java.io.StringReader;

import org.apache.lucene.analysis.cjk.CJKAnalyzer;
import org.apache.lucene.analysis.jjs.CKIPAnalyzer;
import org.apache.lucene.util.AnalyzerUtils;
import org.apache.lucene.util.Version;

import com.chenlb.mmseg4j.ComplexSeg;
import com.chenlb.mmseg4j.Dictionary;
import com.chenlb.mmseg4j.MMSeg;
import com.chenlb.mmseg4j.Seg;
import com.chenlb.mmseg4j.Word;
import com.chenlb.mmseg4j.analysis.MMSegAnalyzer;

public class AnalyzerTest {
	public static void DemoAnalyzer() throws Exception
	{
		/*Properties prop = new Properties();
		prop.setProperty("log4j.rootLogger", "DEBUG, ERROR");
		prop.setProperty("log4j.appender.A1", "org.apache.log4j.ConsoleAppender");
		prop.setProperty("log4j.appender.A1.layout", "org.apache.log4j.PatternLayout");
		prop.setProperty("log4j.appender.A1.layout.ConversionPattern", "%d [%t] %-5p %c - %m%n");
		PropertyConfigurator.configure(prop);*/		
		System.out.printf("\t[Info] MMSegAnlayzer:\n");
		AnalyzerUtils.displayTokensWithFullDetails(new MMSegAnalyzer(), "今天我要去學校");
		System.out.println();
		
		System.out.printf("\t[Info] CKJAnalyzer:\n");
		AnalyzerUtils.displayTokensWithFullDetails(new CJKAnalyzer(Version.LUCENE_30), "今天我要去學校");
		System.out.println();
		
		System.out.printf("\t[Info] CKIPAnalyzer:\n");
		AnalyzerUtils.displayTokensWithFullDetails(new CKIPAnalyzer(), "今天我要去學校");
		System.out.println();
	}
	
	public static void DemoMmseg() throws Exception
	{
		String sent = "我今天要去公司,找主管拿明天要用的資料";
		Seg seg = new ComplexSeg(Dictionary.getInstance());
		MMSeg mmSeg = new MMSeg(new StringReader(sent), seg);
		Word word = null;
		while((word=mmSeg.next())!=null) {
			char [] chars = word.getSen();
			int wet = word.getWordOffset();
			int len = word.getLength();
			//for(char c:chars) System.out.printf("%c", c);
			System.out.printf("%s:(%s/%d/%d) %d-%d\n",  word.getString(),
					                              word.getType(),
					                              wet,
					                              len,
					                              word.getStartOffset(),
					                              word.getEndOffset());
			
		}
		System.out.println(); 
	}
	
	/**
	 * REF:
	 * 	- mmseg4j: https://code.google.com/p/mmseg4j/
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		DemoAnalyzer();
		//DemoMmseg();
	}
}
